package seqlist.queue.leetcode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 单队列实现的栈
 * @author yuisama
 * @date 2022/05/10 20:15
 **/
public class Num225_SingeQueueImplStack {
    private Queue<Integer> queue = new LinkedList<>();

    public void push(int x) {
        // 1.记录下当前队列的元素个数size
        int size = queue.size();
        // 2.将新元素直接入队
        queue.offer(x);
        // 3.将原先的前n个元素依次出队再入队，新元素就变成了队首元素
        for (int i = 0; i < size; i++) {
            queue.offer(queue.poll());
        }
    }

    public int pop() {
        return queue.poll();
    }

    public int top() {
        return queue.peek();
    }

    public boolean empty() {
        return queue.isEmpty();
    }
}